<?php
// @formatter:off
/**
 * @file 2.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-07 02:16:25 +0000 (Fri, 07 Jun 2013) $
 *
 * $Id: 2.php 71 2013-06-07 02:16:25Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/install/steps/2.php $
 */
// @formatter:on

if(defined('__INSTALLER__')) {
	$wcComicsHolder = WCComicsHolder::Instance();
	$wcDirectoriesHolder = WCDirectoriesHolder::Instance();
	$wcGroupsHolder = WCGroupsHolder::Instance();
	$groups = $wcGroupsHolder->groupIds();

	if(count($groups) > 0) {
		$aux = "<?php\n";
		$aux .= "\$wcDefaults['group'] = {$groups[0]};\n";
		$aux .= "?>\n";

		file_put_contents($wcPaths['install-tmps']['group'], $aux);

		echo "\t\t\t\t<p>First group already created.</p>\n";
		$isOkToContinue = true;
		$isOkToReload = true;
	} else {
		if($_SERVER["REQUEST_METHOD"] != "POST") {
			echo "\t\t\t\t<p>This is step {$wciStepCheck}. Here you will configure your first group of comics.</p>\n";
			echo "\t\t\t\t<p><form action=\"#\" method=\"post\">\n";
			echo "\t\t\t\t\t<ul>\n";
			echo "\t\t\t\t\t\t<li><label>Group Name</label>:<input type=\"text\" id=\"GP_NAME\" name=\"GP_NAME\" value=\"My Comics\"/></li>\n";
			echo "\t\t\t\t\t\t<li><label>Group Directory</label>:<input type=\"text\" id=\"GP_PATH\" name=\"GP_PATH\" value=\"\" title=\"It must be a full path\"/></li>\n";
			echo "\t\t\t\t\t\t<li><label>Group Trash Directory</label>:<input type=\"text\" id=\"GP_TRASH\" name=\"GP_TRASH\" value=\"\" title=\"It must be a relative path\"/></li>\n";
			echo "\t\t\t\t\t</ul>\n";
			echo "\t\t\t\t\t<input type=\"submit\" value=\"Send\"/>\n";
			echo "\t\t\t\t\t<input type=\"reset\"  value=\"Reset\"/>\n";
			echo "\t\t\t\t</form></p>\n";

			$isOkToContinue = false;
			$isOkToReload = false;
		} else {
			$name = $_POST["GP_NAME"];
			$directory = $_POST["GP_PATH"];
			$trash = $_POST["GP_TRASH"];

			$directory = WCSanitizer::SystemPath($directory);

			if($trash) {
				$trash = WCSanitizer::WComixPath($trash);
			}

			if(is_dir($directory)) {
				$trashPath = WCSanitizer::SystemPath("{$directory}{$trash}");
				if($trash != "" && !is_dir($trashPath)) {
					echo "\t\t\t\t<p>[FL:Trash directory '{$trashPath}' doesn't exists.]</p>\n";
					$isOkToContinue = false;
					$isOkToReload = true;
				} else {
					$id = $wcGroupsHolder->add($name, $directory, $trash);
					$group = $wcGroupsHolder->groupById($id);
					if($id !== false && $group) {
						$group->setPublic(true);

						$aux = "<?php\n";
						$aux .= "\$wcDefaults['group'] = {$id};\n";
						$aux .= "?>\n";

						file_put_contents($wcPaths['install-tmps']['group'], $aux);

						echo "\t\t\t\t<p>[OK:Group created.]</p>\n";
					} else {
						echo "\t\t\t\t<p>[FL:Groups couldn't be created.]</p>\n";
						$isOkToContinue = false;
						$isOkToReload = true;
					}
				}
			} else {
				echo "\t\t\t\t<p>[FL:Directory '{$directory}' doesn't exists.]</p>\n";
				$isOkToContinue = false;
				$isOkToReload = true;
			}
		}
	}
}
?>